package com.cgbl.service.ui

import android.os.Bundle
import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.service.BR
import com.cgbl.service.R
import com.cgbl.service.adapter.TeamManagerAdapter
import com.cgbl.service.assist.ChatAssist
import com.cgbl.service.databinding.AppActivitySelectTeamBinding
import com.cgbl.service.vm.SelectTeamViewModel
import com.netease.nimlib.sdk.team.model.CreateTeamResult
import com.netease.yunxin.kit.alog.ALog
import com.netease.yunxin.kit.conversationkit.ui.page.PopItemFactory
import com.netease.yunxin.kit.corekit.model.ResultInfo
import com.netease.yunxin.kit.corekit.model.ResultObserver
import com.netease.yunxin.kit.corekit.route.XKitRouter.withKey
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.TeamListBean

@Route(path = AppConstants.Router.Project.A_SELECT_TEAM)
class SelectTeamActivity : GLBaseActivity<AppActivitySelectTeamBinding, SelectTeamViewModel>() {

    lateinit var teamManagerAdapter: TeamManagerAdapter

    var selectedIds = arrayListOf<String>()

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_select_team

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        teamManagerAdapter = TeamManagerAdapter(true)
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = teamManagerAdapter
        }
//        teamManagerAdapter.setOnItemClickListener { _, _, position ->
//            ARouterUtil.goToActivityWithBundle(AppConstants.Router.Project.A_ADD_TEAM,
//                Bundle().apply {
//                    putString(AppConstants.BundleKey.ID, teamManagerAdapter.data[position].id)
//                })
//        }
        binding.clear.setOnClickListener {
            teamManagerAdapter.cancelAllCheck()
        }
        binding.complete.setOnClickListener {
            val member = teamManagerAdapter.getSelectedMember() ?: return@setOnClickListener
            viewModel.fetchTeamProfile(member)
        }
        viewModel.fetchHomeTeam()
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView?.text = "选择团队"
        backView?.setOnClickListener { finish() }
    }

    override fun initViewObservable() {
        viewModel.uc.homeTeamDataEvent.observe(this) {
            teamManagerAdapter.setNewInstance(it as MutableList<TeamListBean>?)
        }
        viewModel.uc.teamProfileDataEvent.observe(this) {
            ChatAssist.createGroup(it) {
                finishActivity()
            }
        }
    }

    override fun initParam() {
        selectedIds = intent.getStringArrayListExtra(AppConstants.BundleKey.IDS) ?: arrayListOf()
    }
}